﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Data;
using QuickWeb.Resources;

namespace QuickWeb
{
    public partial class SettingsPage : PhoneApplicationPage
    {

        private AppSettings settings;

        public SettingsPage()
        {
            settings = new AppSettings();
            InitializeComponent();
            //Bind search engine setting
            Binding settingsBinding = new Binding();
            settingsBinding.Path = new PropertyPath("SearchEngine");
            settingsBinding.Mode = BindingMode.TwoWay;
            settingsBinding.Source = settings;
            //browserSelectList.SetBinding(ListPicker.SelectedIndexProperty, settingsBinding);
            //Bind starPivot setting
            Binding starPivotBinding = new Binding();
            starPivotBinding.Path = new PropertyPath("StartPivot");
            starPivotBinding.Mode = BindingMode.TwoWay;
            starPivotBinding.Source = settings;
            startPivotSelectList.SetBinding(ListPicker.SelectedIndexProperty, starPivotBinding);
            //Update feeds on open setting
            if (settings.UpdateFeedsAutomatically)
            {
                UpdateFeedsOnOpenToggle.Content = AppResources.Enabled;
            }
            else
            {
                UpdateFeedsOnOpenToggle.Content = AppResources.OnOpen;
            }
            Binding openFeedsOnOpenBinding = new Binding();
            openFeedsOnOpenBinding.Path = new PropertyPath("UpdateFeedsAutomatically");
            openFeedsOnOpenBinding.Mode = BindingMode.TwoWay;
            openFeedsOnOpenBinding.Source = settings;
            UpdateFeedsOnOpenToggle.SetBinding(ToggleSwitch.IsCheckedProperty, openFeedsOnOpenBinding);
            //Load images setting
            LoadImagesList.SelectedIndex = settings.LoadFeedsImages;            
            Binding loadImagesBinding = new Binding();
            loadImagesBinding.Path = new PropertyPath("LoadFeedsImages");
            loadImagesBinding.Mode = BindingMode.TwoWay;
            loadImagesBinding.Source = settings;
            LoadImagesList.SetBinding(ListPicker.SelectedIndexProperty, loadImagesBinding);
            //Longitud maxima de las preview
            SelectPreviewMaxLengthList.SelectedIndex = settings.ShowPreviewMaxLength;
            Binding previewLenBinding = new Binding();
            previewLenBinding.Path = new PropertyPath("ShowPreviewMaxLength");
            previewLenBinding.Mode = BindingMode.TwoWay;
            previewLenBinding.Source = settings;
            SelectPreviewMaxLengthList.SetBinding(ListPicker.SelectedIndexProperty, previewLenBinding);
            //Motor de busqueda por defecto
            browserSelectList.SelectedIndex = settings.ShowPreviewMaxLength;
            Binding searchEngineBinding = new Binding();
            searchEngineBinding.Path = new PropertyPath("SearchEngine");
            searchEngineBinding.Mode = BindingMode.TwoWay;
            searchEngineBinding.Source = settings;
            browserSelectList.SetBinding(ListPicker.SelectedIndexProperty, searchEngineBinding);

        }

        private void pivot_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void UpdateFeedsOnOpenToggle_Checked(object sender, RoutedEventArgs e)
        {
            UpdateFeedsOnOpenToggle.Content = AppResources.Enabled;
        }

        private void UpdateFeedsOnOpenToggle_Unchecked(object sender, RoutedEventArgs e)
        {
            UpdateFeedsOnOpenToggle.Content = AppResources.OnOpen;
        }

        //private void LoadImagesToggle_Checked(object sender, RoutedEventArgs e)
        //{
        //    LoadImagesToggle.Content = AppResources.Enabled;
        //}

        //private void LoadImagesToggle_Unchecked(object sender, RoutedEventArgs e)
        //{
        //    LoadImagesToggle.Content = AppResources.Disabled;
        //}

       
    }
}